<template>
  <div class="app-container">

    <!-- 条件查询 -->
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="用户ID:">
        <el-input v-model.trim="query.userId"></el-input>
      </el-form-item>
      <el-form-item label="用户名:">
        <el-input v-model.trim="query.username"></el-input>
      </el-form-item>
      <el-form-item label="手机号:">
        <el-input v-model.trim="query.mobile"></el-input>
      </el-form-item>
      <el-form-item label="邮箱:">
        <el-input v-model.trim="query.email"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="list"
      stripe
      border
      style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="60"></el-table-column>
      <el-table-column align="center" prop="userId" label="用户Id" width="80"></el-table-column>
      <el-table-column align="center" prop="username" label="用户名"></el-table-column>
      <el-table-column align="center" prop="mobile" label="手机号"></el-table-column>
      <el-table-column align="center" prop="email" label="邮箱"></el-table-column>
      <el-table-column align="center" prop="loginTime" label="登录时间"></el-table-column>
      <el-table-column align="center" prop="loginIp" label="登录IP"></el-table-column>
      <el-table-column align="center" prop="userAgent" label="登录终端"  width="400"></el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 50]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
    </el-pagination>
  </div>
</template>

<script>

import * as api from '@/api/logrecord'

export default {
  name: 'LogRecord', // 和对应路由表中配置的name值一致

  data() {
    return {
      list: [],
      page: {
        current: 1,
        size: 20,
        total: 0
      },

      query: {}
    }
  },

  created() {
    this.fetchData()
  },

  methods: {
    async fetchData() {
      const {data} = await api.getList(this.query, this.page.current, this.page.size)
      this.list = data.rows
      this.page.total = data.total
    },

    // val 是切换之后的每页显示多少条
    handleSizeChange(val) {
      this.page.size = val
      this.fetchData()
    },

    // 当页码改变后触发到此方法，val 是当前点击（或输入）的那个页码，
    handleCurrentChange(val) {
      this.page.current = val
      this.fetchData()
    },

    // 条件查询
    queryData() {
      // 将页码变为1，第1页
      this.page.current = 1
      this.fetchData()
    },

    // 重置
    reload() {
      this.query = {}
      this.fetchData()
    }
  }
}
</script>
